package com.pb.camera;

import java.util.Timer;
import java.util.TimerTask;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.pb.camera.application.App;
import com.pb.camera.utils.Constants;
import com.pb.camera.utils.JsonAnalyle;
import com.pb.camera.utils.MyTextWatcher;
import com.pb.camera.utils.RegexValidateUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WeiXinRegisterActivity  extends Activity implements  OnClickListener{
	private   EditText  edit_account;
	private EditText  edit_verrification_code;
	private EditText  edit_email;
	private  ImageView image_account;
	private ImageView  image_email;
	private ImageView image_back;
	private TextView  text_timer;
	private LinearLayout  line_sendcode;
	private LinearLayout linear_next;
	private boolean hasName=false;
	private String openId="";
	private Timer timer;
	private Handler  handler=new Handler(){
		public void handleMessage(Message msg) {
			if (msg.arg1!=0)
			{	
				  text_timer.setText(msg.arg1+"秒后重新发送");
				  line_sendcode.setClickable(false);		
			}
			else 
			{
				 text_timer.setText("重新发送");
				 line_sendcode.setClickable(true);
			}
			if (msg.arg2==6) {
				 Toast.makeText(WeiXinRegisterActivity.this,(String) msg.obj, Toast.LENGTH_SHORT).show();
				
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 requestWindowFeature(Window.FEATURE_NO_TITLE); 
		 
		setContentView(R.layout.weiixn_register);
		  if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
	            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
	          //  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
	        }
		App.getInstance().getAllActivity().add(this);
		initView();
	}
  
	private void initView()
	{      final View  view=findViewById(R.id.weixin_register_image_padd);
		   final int height= App.getInstance().getStatusBarHeight(this);
           view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {					
				@Override
				public boolean onPreDraw() {
					// TODO Auto-generated method stub
					view.setPadding(0, height, 0, 0);
					view.getViewTreeObserver().removeOnPreDrawListener(this);
					return true;
				}
			});
	    openId  =getIntent().getExtras().getString("openId");
		edit_account=(EditText) findViewById(R.id.weixin_register_edit_account);
		edit_email=(EditText) findViewById(R.id.weixin_register_edit_email);
		edit_verrification_code=(EditText) findViewById(R.id.weixin_register_edit_code);
		image_back=(ImageView) findViewById(R.id.weixin_register_image_back);
		image_back.setOnClickListener(this);
		image_account=(ImageView)findViewById(R.id.weixin_register_image_reset_phonenum);
		image_email =(ImageView) findViewById(R.id.weixin_register_image_reset_email);
		edit_account.addTextChangedListener(new MyTextWatcher(edit_account, image_account));
		edit_email.addTextChangedListener(new MyTextWatcher(edit_email, image_email));
		text_timer=(TextView) findViewById(R.id.weixin_register_textviwe_timer);
		line_sendcode=(LinearLayout)findViewById(R.id.weixin_register_line_sendcode);
		line_sendcode.setOnClickListener(this);
		linear_next=(LinearLayout) findViewById(R.id.weixin_register_line_next);
		linear_next.setOnClickListener(this);
		
	}
	
	 private void sendVerrificationCode(final View view)
	  {
		  if(!RegexValidateUtil.checkMobileNumber(edit_account.getText().toString()))	  
		  {
			  Toast.makeText(this, "电话号码的格式不对", Toast.LENGTH_LONG).show();
		  }
		  else 
		  {	//保存电话号码
			  if(timer!=null)
			  {
				  //防止多次点击获取验证码
				 view.setClickable(false);	  
			  }
			  else 
			  {
					timer=new Timer();	  
					 view.setClickable(false);	
			  }  
			  
			TimerTask  task=new TimerTask() {
			   private 	  int time=60;
				@Override
				public void run() {
					// TODO Auto-generated method stub
		        	time--;
		           Message mes=Message.obtain();
		           if(mes==null)
		           {
		        	   mes=new Message();
		           }
		           mes. arg1=time;
		           handler.sendMessage(mes);
				//   textView_timer.setText(time+"秒后重新发送");
				   if(time==0)
				   {
		           cancel();
				  }
				}
			};		
			 timer.schedule(task, 0, 1000);	
			//获取短息的验证码的代码位置
 	 getCode(Constants.getCodeUrl(edit_account.getText().toString(), "1"), this, 0);
		  }	    
	  }	  
	 
	 
	 
	   private void   getCode(String  url,final Activity  activity,final int mode)
		  {
			   HttpUtils httpUtils = new HttpUtils();
			   RequestParams params=new RequestParams();
				httpUtils.send(HttpMethod.POST, url,  new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Toast.makeText(activity, "网络访问失败,请检查你的网络", 0).show();
					}
					@Override
					public void onSuccess(ResponseInfo<String> arg0) {	
						boolean flag=  JsonAnalyle.jsonAnalyse(arg0.result, WeiXinRegisterActivity.this, null,handler);
						if(flag)
						{
							if (mode==1)
							{ //验证码输入正确
							 //那就开始设置手势
								Bundle bundle=new Bundle();
								bundle.putString("whereFrom", "weixin");
								bundle.putString("mobile", edit_account.getText().toString());
								bundle.putString("email", edit_email.getText().toString());
								bundle.putString("openId", openId);
								bundle.putString("code", edit_verrification_code.getText().toString());
								Intent intent=new Intent(WeiXinRegisterActivity.this,CreateGesturePasswordActivity.class);
								intent.putExtras(bundle);
								startActivity(intent);			
							}
							else if(mode==0)
							{//成功获得验证码的时候
																		
							}							
						}
					}				
				});			
		  }
	   	      
	   private  boolean    yanzhengEmail()
	   {	   					
				 if(! RegexValidateUtil.checkEmail(edit_email.getText().toString()))
					{
						Toast.makeText(this, "邮件格式不对", Toast.LENGTH_SHORT).show();
						return   false;
					}		
				
				return   true;
	 }

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
		switch (view.getId()) {			
				case R.id.weixin_register_rootview:
					  InputMethodManager imm = (InputMethodManager)
					  getSystemService(Context.INPUT_METHOD_SERVICE);
					  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);    			
				break;		
				case R.id.weixin_register_line_next:
					boolean  flag=yanzhengEmail();
					if (flag) {						
				 //需要确认验证码的正确性码
				 //开启设置手势密码的界面	
			       getCode(Constants.compareCodeUrl(edit_account.getText().toString(), edit_verrification_code.getText().toString()), this,1);	             
					}			
					break;		
				case R.id.weixin_register_image_back:
					finish();
					break;
				case R.id.weixin_register_line_sendcode:				
					sendVerrificationCode(view);
					break;
				default:
					break;
				}	
	
	}			

	@Override
	public void finish() {
		App.getInstance().getAllActivity().remove(this);
		super.finish();
	}
	@Override
	protected void onDestroy() {
		App.getInstance().getAllActivity().remove(this);
		super.onDestroy();
	}   
	   
}

